package com.hjm.project.transactionReward.service.impl;

import java.util.List;

import com.hjm.common.exception.BusinessException;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import com.hjm.project.transactionReward.mapper.TransactionRewardMapper;
import com.hjm.project.transactionReward.domain.TransactionReward;
import com.hjm.project.transactionReward.service.ITransactionRewardService;
import com.hjm.common.utils.text.Convert;

import javax.annotation.Resource;

/**
 * 交易量奖励配置Service业务层处理
 * 
 * @author maker-platform
 * @date 2024-01-19
 */
@Service
public class TransactionRewardServiceImpl implements ITransactionRewardService {
    @Resource
    private TransactionRewardMapper transactionRewardMapper;

    /**
     * 查询交易量奖励配置
     * 
     * @param id 交易量奖励配置ID
     * @return 交易量奖励配置
     */
    @Override
    public TransactionReward selectTransactionRewardById(Integer id) {
        return transactionRewardMapper.selectTransactionRewardById(id);
    }

    /**
     * 查询交易量奖励配置列表
     * 
     * @param transactionReward 交易量奖励配置
     * @return 交易量奖励配置
     */
    @Override
    public List<TransactionReward> selectTransactionRewardList(TransactionReward transactionReward) {
        return transactionRewardMapper.selectTransactionRewardList(transactionReward);
    }

    /**
     * 查询交易量奖励配置列表
     *
     * @param transactionReward 交易量奖励配置
     * @return 交易量奖励配置 一个结果
     */
    @Override
    public TransactionReward selectTransactionRewardOne(TransactionReward transactionReward) {
        return transactionRewardMapper.selectTransactionRewardOne(transactionReward);
    }

    /**
     * 新增交易量奖励配置
     * 
     * @param transactionReward 交易量奖励配置
     * @return 结果
     */
    @Override
    public int insertTransactionReward(TransactionReward transactionReward) {
        TransactionReward t = new TransactionReward();
        t.setAgentNum(transactionReward.getAgentNum());
        t.setType(transactionReward.getType());
        if (transactionReward.getRewardType() == 0) {
            t.setActId(transactionReward.getActId());
        }
        t.setRewardType(transactionReward.getRewardType());
        List<TransactionReward> transactionRewards = transactionRewardMapper.selectTransactionRewardList(t);
        if (CollectionUtils.isNotEmpty(transactionRewards)){
            throw new BusinessException("奖励规则已配置");
        }
        return transactionRewardMapper.insertTransactionReward(transactionReward);
    }

    /**
     * 修改交易量奖励配置
     * 
     * @param transactionReward 交易量奖励配置
     * @return 结果
     */
    @Override
    public int updateTransactionReward(TransactionReward transactionReward) {
        return transactionRewardMapper.updateTransactionReward(transactionReward);
    }

    /**
     * 删除交易量奖励配置对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTransactionRewardByIds(String ids) {
        String[] strArray = Convert.toStrArray(ids);
        for (String ss : strArray) {
            TransactionReward transactionReward = transactionRewardMapper.selectTransactionRewardById(Integer.parseInt(ss));
            if (transactionReward.getStatus() == 1) {
                throw new BusinessException("奖励规则已开启,不可删除");
            }
        }
        return transactionRewardMapper.deleteTransactionRewardByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除交易量奖励配置信息
     * 
     * @param id 交易量奖励配置ID
     * @return 结果
     */
    @Override
    public int deleteTransactionRewardById(Integer id) {
        return transactionRewardMapper.deleteTransactionRewardById(id);
    }
}
